package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;

/**
 * Created by Administrator on 2016.8.2.
 */
public class Test16_11 extends JFrame{
    public Test16_11(){
        add(new MovingMessagePane("message moving?"));
    }

    public static void main(String args[]){
        Test16_11 frame=new Test16_11();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(280,100);
        frame.setTitle("AnimationDemo");
        frame.setVisible(true);
    }

    static class MovingMessagePane extends JPanel{
        private String message="Welcome to Jave";
        private int xCoordinate=0;
        private int yCoordinate=20;

        public MovingMessagePane(String message){
            this.message=message;

            Timer timer=new Timer(100,new TimerListener());
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if(xCoordinate>getWidth()){
                xCoordinate=-20;
            }
            xCoordinate+=5;
            g.drawString(message,xCoordinate,yCoordinate);
        }

        class TimerListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                repaint();
            }
        }
    }
}
